package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.logic.TransactionManager;
import com.moleq.models.SetFunction;
import com.moleq.mgpos.activities.R;

public class SelectFuncSourceDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button mButtonBack,mButtonOk;
	private TransactionManager mPaymentMgr;
	private Context mContext;
	private ListView mListView;
	private List<SetFunction> mDataSource = new  ArrayList<SetFunction>();
	private OpenCouponAdapter mAdapter;
	private int selectedRow = 0;
	private String value;
	private int flag = 0;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.btn_source_cancel:
				dismiss();
				break;
			case R.id.btn_source_ok:	
				if(mDataSource.size() <= 0){
					return;	
				}
				onCloseWithReturn(mDataSource.get(selectedRow));
				dismiss();
				break;
			}
		}
	};
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_list:
				mAdapter.ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public void onCloseWithReturn(Object obj){
//		mOnResultListener.onReturnValue(0,obj);
		mOnResultListener.onReturnValue(flag,obj);
		dismiss();
	}
	
	public SelectFuncSourceDialog(Context context) {
		super(context,context);
		constructor(context);
		
	}
	public SelectFuncSourceDialog(Context context, Object base) {
		super(context,base);
		constructor(context,base);
	}
	
	public void setSource(List<SetFunction> mDataSource,String value){
		this.mDataSource = mDataSource;
		this.value = value;
	}
	
	public void setSource(List<SetFunction> mDataSource,String value,int flag){
		this.mDataSource = mDataSource;
		this.value = value;
		this.flag = flag;
	}
	
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public SelectFuncSourceDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_string_source);
		this.setCancelable(false);
	
		mButtonBack = (Button) findViewById(R.id.btn_source_cancel);
		mButtonBack.setOnClickListener(mOnClickListener);
		
		mButtonOk = (Button) findViewById(R.id.btn_source_ok);
		mButtonOk.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_list);
		mListView.setOnItemClickListener(onItemClickListener);
		
		mAdapter = new OpenCouponAdapter(mContext);
		mListView.setAdapter(mAdapter);
	}
	
	public void show(){
		super.show();	
		
		Locate(value);
	}
	
	public void Locate(String value) {
		for (int i = 0; mDataSource.size() > i; i++) {
			if (mDataSource.get(i).getValue().equals(value)) {
				mAdapter.ScrollToRow(i);
				return;
			}
		}
		mAdapter.ScrollToRow(0);
	}
	
	class OpenCouponAdapter extends BaseAdapter{
		private Context maContext;
		public final class ViewHolder{
			public TextView tv_selection_unit;
		}
		
		public OpenCouponAdapter(Context context){
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				
				convertView = LayoutInflater.from(maContext).inflate(R.layout.selection_items, null);
				holder.tv_selection_unit = (TextView) convertView.findViewById(R.id.tv_selection_name);

				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tv_selection_unit.setText(mDataSource.get(position).getName());
			
			if(position == selectedRow){
				holder.tv_selection_unit.setBackgroundColor(Color.rgb(255,219,16));
			}else{
				holder.tv_selection_unit.setBackgroundColor(Color.rgb(204, 204, 204));
			}
			return convertView;
		}
		
		public void ScrollToRow(int row){
			selectedRow = row;
			mAdapter.notifyDataSetChanged();
		}
	}
}
